package com.peakmain.homemodule.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.peakmain.baselibrary.recyclerview.adapter.CommonRecyclerAdapter;
import com.peakmain.baselibrary.recyclerview.adapter.MultiTypeSupport;
import com.peakmain.baselibrary.recyclerview.adapter.ViewHolder;
import com.peakmain.homemodule.R;
import com.peakmain.homemodule.bean.SearchCityBean;

import java.util.List;

/**
 * author ：Peakmain
 * createTime：2020/2/28
 * mail:2726449200@qq.com
 * describe：
 */
public class SearchCityAdapter extends CommonRecyclerAdapter<SearchCityBean> {

    private LinearLayoutManager mLayoutManager;

    public SearchCityAdapter(Context context, List<SearchCityBean> data) {
        super(context, data, new MultiTypeSupport<SearchCityBean>() {
            @Override
            public int getLayoutId(SearchCityBean item, int position) {
                return R.layout.default_city_recycler_item;
            }
        });
    }

    @Override
    public void convert(ViewHolder holder, SearchCityBean item) {
        int itemViewType = getItemViewType(holder.getAdapterPosition());
        if (itemViewType == R.layout.default_city_recycler_item) {
            holder.setText(R.id.tv_city_name, item.getName());
        }


    }

    public void setLayoutManager(LinearLayoutManager manager) {
        this.mLayoutManager = manager;
    }



    public void scrollToSection(String index) {
        if (mData == null || mData.isEmpty()) {
            return;
        }
        if (TextUtils.isEmpty(index)) {
            return;
        }
        int size = mData.size();
        for (int i = 0; i < size; i++) {
            if (TextUtils.equals(index.substring(0, 1), mData.get(i).getSection().substring(0, 1))) {
                if (mLayoutManager != null) {
                    mLayoutManager.scrollToPositionWithOffset(i, 0);
                    if (TextUtils.equals(index.substring(0, 1), "定")) {
                        //防止滚动时进行刷新
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                notifyItemChanged(0);
                            }
                        }, 1000);

                    }
                    //不然会滚动到最后一条
                    return;
                }
            }
        }

    }
}
